/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.mywork.potentialappointmentinfo.web;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.appointment.entity.PotentialAppointmentInfo;
import cn.knet.sp.ncrm.modules.customer.appointment.service.PotentialAppointmentInfoService;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 预约信息Controller
 * @author shishouyang
 * @version 2015-09-10
 */
@Controller
@RequestMapping(value = "${adminPath}/mywork/potentialAppointmentInfo")
public class MyPotentialAppointmentInfoController extends BaseController {

	@Autowired
	private PotentialAppointmentInfoService potentialAppointmentInfoService;

	@ModelAttribute
	public PotentialAppointmentInfo get(@RequestParam(required=false) String id) {
		PotentialAppointmentInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = potentialAppointmentInfoService.get(id);
		}
		if (entity == null){
			entity = new PotentialAppointmentInfo();
		}
		return entity;
	}

	@RequiresPermissions("mywork:potentialAppointmentInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(PotentialAppointmentInfo potentialAppointmentInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialAppointmentInfo> page = potentialAppointmentInfoService.findPage(new Page<PotentialAppointmentInfo>(request, response), potentialAppointmentInfo);
		model.addAttribute("page", page);
		return "modules/mywork/potentialappointmentinfo/potentialAppointmentInfoAjaxList";//ajax模式
	}

	@RequiresPermissions("mywork:potentialAppointmentInfo:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(PotentialAppointmentInfo potentialAppointmentInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		long thisTime = System.currentTimeMillis();
		if("1".equals(potentialAppointmentInfo.getStatus())){
			potentialAppointmentInfo.setEndAppointmentDate(new Date(thisTime));
		}else if("2".equals(potentialAppointmentInfo.getStatus())){
			potentialAppointmentInfo.setStartAppointmentDate(new Date(thisTime));
			potentialAppointmentInfo.setEndAppointmentDate(new Date(thisTime+3*24*60*60*1000));
		}else if("3".equals(potentialAppointmentInfo.getStatus())){
			potentialAppointmentInfo.setStartAppointmentDate(new Date(thisTime+3*24*60*60*1000));
		}

		potentialAppointmentInfo.setUserId(UserUtils.getUser().getId());
		Page<PotentialAppointmentInfo> page = potentialAppointmentInfoService.findPage(new Page<PotentialAppointmentInfo>(request, response), potentialAppointmentInfo);
		return page;
	}

	@RequiresPermissions("mywork:potentialAppointmentInfo:view")
	@RequestMapping(value = "form")
	public String form(PotentialAppointmentInfo potentialAppointmentInfo, Model model) {
		potentialAppointmentInfo.addHistoryFlag();
		model.addAttribute("potentialAppointmentInfo", potentialAppointmentInfo);
		return "modules/mywork/potentialappointmentinfo/potentialAppointmentInfoForm";
	}

	@RequiresPermissions("mywork:potentialAppointmentInfo:edit")
	@RequestMapping(value = "save")
	public String save(PotentialAppointmentInfo potentialAppointmentInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, potentialAppointmentInfo)){
			return form(potentialAppointmentInfo, model);
		}
		Date appointmentDate = potentialAppointmentInfo.getAppointmentDate();
		long advanceMin = potentialAppointmentInfo.getAdvanceMin();
		potentialAppointmentInfo.setNoticeDate(new Date(appointmentDate.getTime()-advanceMin*60*1000));
		potentialAppointmentInfo.setLastUpdated(new Date());
		potentialAppointmentInfoService.save(potentialAppointmentInfo);
		addMessage(redirectAttributes, "保存预约信息成功");
		return "redirect:"+Global.getAdminPath()+"/mywork/potentialAppointmentInfo/?repage";
	}

	//ajax模式 删除
	@RequiresPermissions("mywork:potentialAppointmentInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(PotentialAppointmentInfo potentialAppointmentInfo, RedirectAttributes redirectAttributes) {
		AjaxMessage result = AjaxMessage.success();
		try {
			potentialAppointmentInfoService.delete(potentialAppointmentInfo);
			result.appendMessage("删除预约信息成功!");
		} catch (Exception e) {
			logger.warn("删除预约信息失败", e);
			result.makeError("删除预约信息失败!系统发生异常");
		}
		return result;
	}

	/**
	 * 用户数据提交接口
	 *
	 * @throws Exception
	 * @author shishouyang
	 * @date 2015-09-09 11:29:12
	 */
	@RequestMapping(value = "getRemindNum")
	@ResponseBody
	public int getRemindNum() {
		int remindNum = 0;
		long thisTime = System.currentTimeMillis();
		PotentialAppointmentInfo info = new PotentialAppointmentInfo();
		info.setNoticeDate(new Date(thisTime));
		info.setUserId(UserUtils.getUser().getId());
		try{
			remindNum = potentialAppointmentInfoService.getRemindNum(info);
		}catch (Exception e){

			logger.error("系统获取预约回访数据提醒错误");
		}
		return remindNum;
	}

}